# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2021/9/30 15:26
# @Author  : zuodengbo
# @Email   : zuodb@amoydx.com
# @File    : resp_code
# @Software: PyCharm
import json
from sanic import response
from sanic.response import file_stream

from utils.common import DateEncoder
from utils.const import RespCode


def make_file_response(file):
    """
    返回文件响应
    :return:
    """
    return file_stream(file)


def make_success_response(message="Success !", data=None, headers=None, **kwargs):
    """
    成功的响应
    :param headers:
    :param message:
    :param data:
    :return:
    """
    template_code = dict()
    template_code["code"] = RespCode.SUCCESS
    template_code["message"] = message
    template_code.update(kwargs)
    if data or isinstance(data, dict) or isinstance(data, list):
        template_code.update(data=data)
    template_code = json.loads(json.dumps(template_code, cls=DateEncoder))
    if headers:
        return response.json(template_code, headers=headers)

    return response.json(template_code)


def make_error_response(code, message):
    """
    失败的响应
    :return:
    """
    template_code = dict()
    template_code["code"] = code
    template_code["message"] = message

    return response.json(template_code)


def make_forbidden_response(message=None):
    """forbidden response"""
    template_code = dict()
    template_code["code"] = RespCode.FORBIDDEN
    template_code["message"] = message or "Forbidden !"
    return response.json(template_code, status=RespCode.FORBIDDEN)


def make_unprocessable_response(data=None, message=None):
    """unprocessable response"""
    template_code = dict()
    if data is None:
        data = {}
    template_code["code"] = RespCode.UNPROCESSABLE_ENTITY
    template_code["message"] = message or "Unprocessable Entity !"
    template_code["data"] = data
    return response.json(template_code, status=RespCode.UNPROCESSABLE_ENTITY)


def make_not_found_response(message=None):
    """not found response"""
    template_code = dict()
    template_code["code"] = RespCode.NOT_FOUND
    template_code["message"] = message or "Url Not Found !"
    return response.json(template_code, status=RespCode.NOT_FOUND)


def make_method_not_allowed_response(message=None):
    """
    Method Not Allowed
    :param message:
    :return:
    """
    template_code = dict()
    template_code["code"] = RespCode.NOT_ALLOWED
    template_code["message"] = message or "Method Not Allowed !"
    return response.json(template_code, status=RespCode.NOT_ALLOWED)


if __name__ == '__main__':
    d = {
        "username": "zuo",
        "user_id": 1
    }
    t = 1
    res = make_success_response(data=d, total=t)
    print(res.status, res.body)
